<template>
  <el-dialog :visible.sync="visible" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmitHandle()" :label-width="$i18n.locale === 'en-US' ? '120px' : '80px'">
      <el-form-item label="UUID" prop="id">
        <el-input v-model="dataForm.id" placeholder="UUID"></el-input>
      </el-form-item>
      <el-form-item label="名称" prop="name">
        <el-input v-model="dataForm.name" placeholder="名称"></el-input>
      </el-form-item>
      <el-form-item label="简单介绍" prop="subtext">
        <el-input v-model="dataForm.subtext" placeholder="简单介绍"></el-input>
      </el-form-item>
      <el-form-item label="介绍" prop="context">
        <el-input v-model="dataForm.context" placeholder="介绍"></el-input>
      </el-form-item>
      <el-form-item label="图片" prop="imgurl">
        <el-input v-model="dataForm.imgurl" placeholder="图片"></el-input>
      </el-form-item>
      <el-form-item label="分类 ab_info" prop="type">
        <el-input v-model="dataForm.type" placeholder="分类 ab_info"></el-input>
      </el-form-item>
      <el-form-item label="共享方式  0无条件开放  1-有条件开放" prop="shareType">
        <el-input v-model="dataForm.shareType" placeholder="共享方式  0无条件开放  1-有条件开放"></el-input>
      </el-form-item>
      <el-form-item label="上线时间" prop="onlineDate">
        <el-input v-model="dataForm.onlineDate" placeholder="上线时间"></el-input>
      </el-form-item>
      <el-form-item label="评价计分" prop="goal">
        <el-input v-model="dataForm.goal" placeholder="评价计分"></el-input>
      </el-form-item>
      <el-form-item label="共享形式 1-源码  2-接口 3-组件" prop="shareForm">
        <el-input v-model="dataForm.shareForm" placeholder="共享形式 1-源码  2-接口 3-组件"></el-input>
      </el-form-item>
      <el-form-item label="领域  字典" prop="field">
        <el-input v-model="dataForm.field" placeholder="领域  字典"></el-input>
      </el-form-item>
      <el-form-item label="场景 字典" prop="scene">
        <el-input v-model="dataForm.scene" placeholder="场景 字典"></el-input>
      </el-form-item>
      <el-form-item label="排名" prop="rank">
        <el-input v-model="dataForm.rank" placeholder="排名"></el-input>
      </el-form-item>
      <el-form-item label="部门ID" prop="deptId">
        <el-input v-model="dataForm.deptId" placeholder="部门ID"></el-input>
      </el-form-item>
      <el-form-item label="使用要求" prop="useInfo">
        <el-input v-model="dataForm.useInfo" placeholder="使用要求"></el-input>
      </el-form-item>
      <el-form-item label="0-未删除  1-删除   默认0" prop="isDelete">
        <el-input v-model="dataForm.isDelete" placeholder="0-未删除  1-删除   默认0"></el-input>
      </el-form-item>
      <el-form-item label="是否推荐  1-是  0-否" prop="isUp">
        <el-input v-model="dataForm.isUp" placeholder="是否推荐  1-是  0-否"></el-input>
      </el-form-item>
      <el-form-item label="创建者" prop="creator">
        <el-input v-model="dataForm.creator" placeholder="创建者"></el-input>
      </el-form-item>
      <el-form-item label="创建时间" prop="createDate">
        <el-input v-model="dataForm.createDate" placeholder="创建时间"></el-input>
      </el-form-item>
      <el-form-item label="更新者" prop="updater">
        <el-input v-model="dataForm.updater" placeholder="更新者"></el-input>
      </el-form-item>
      <el-form-item label="更新时间" prop="updateDate">
        <el-input v-model="dataForm.updateDate" placeholder="更新时间"></el-input>
      </el-form-item>
    </el-form>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button>
    </template>
  </el-dialog>
</template>

<script>
import debounce from 'lodash/debounce'
export default {
  data () {
    return {
      visible: false,
      dataForm: {
        id: '',
        name: '',
        subtext: '',
        context: '',
        imgurl: '',
        type: '',
        shareType: '',
        onlineDate: '',
        goal: '',
        shareForm: '',
        field: '',
        scene: '',
        rank: '',
        deptId: '',
        useInfo: '',
        isDelete: '',
        isUp: '',
        creator: '',
        createDate: '',
        updater: '',
        updateDate: '',
      }
    }
  },
  computed: {
    dataRule () {
      return {
      }
    }
  },
  methods: {
    init () {
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.getInfo()
        }
      })
    },
    // 获取信息
    getInfo () {
      this.$http.get('/bsAbility/bsability/' + this.dataForm.id).then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.dataForm = {
          ...this.dataForm,
          ...res.data
        }
      }).catch(() => {})
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.$http[!this.dataForm.id ? 'post' : 'put']('/bsAbility/bsability/', this.dataForm).then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.visible = false
              this.$emit('refreshDataList')
            }
          })
        }).catch(() => {})
      })
    }, 1000, { 'leading': true, 'trailing': false })
  }
}
</script>
